package com.yizhang.newfeatures;

import java.util.ArrayList;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.Predicate;

/**
 * 行为参数化
 */
public class BehavioralParameterization {

    /**
     * 1个参数
     * @param list
     * @param lambda
     * @return
     */
    public static List<Student> filter(List<Student> list, Predicate<Student> lambda) {
        List<Student> students = new ArrayList<>();
        for (Student student : list) {
            if (lambda.test(student)) {
                students.add(student);
            }
        }
        return students;
    }

    /**
     * 2个参数
     * @param list
     * @param lambda
     * @param age
     * @return
     */
    public static List<Student> filter2(List<Student> list, BiPredicate<Student, Integer> lambda, Integer age) {
        List<Student> students = new ArrayList<>();
        for (Student student : list) {
            if (lambda.test(student, age)) {
                students.add(student);
            }
        }
        return students;
    }

    public static void main(String[] args) {
        List<Student> list = List.of(
                new Student(1L, "张无忌", 18),
                new Student(1L, "杨不悔", 16),
                new Student(1L, "周芷若", 19),
                new Student(1L, "赵敏", 20)
        );

        // lambda原始写法
        System.out.println(filter(list, student -> student.getAge() > 18));
        // 方法引用写法 静态方法
        System.out.println(filter(list, BehavioralParameterization::compareAge));
        // 方法引用写法 非静态方法
        BehavioralParameterization bhp = new BehavioralParameterization();
        System.out.println(filter(list, bhp::compareAge1));
        // 在Student类中 非静态方法
        System.out.println(filter(list, Student::compareAge));

        // 2个参数的方法引用
        System.out.println(filter2(list, Student::compareAge, 18));
    }


    public static boolean compareAge(Student student) {
        return student.getAge() > 18;
    }

    public boolean compareAge1(Student student) {
        return student.getAge() > 18;
    }


    public static class Student {
        private Long id;
        private String name;
        private Integer age;

        public Student(Long id, String name, Integer age) {
            this.id = id;
            this.name = name;
            this.age = age;
        }

        public boolean compareAge() {
            return this.age > 18;
        }

        public boolean compareAge(Integer age) {
            return this.age > age;
        }

        public Long getId() {
            return id;
        }

        public void setId(Long id) {
            this.id = id;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        @Override
        public String toString() {
            return "Student{" +
                    "id=" + id +
                    ", name='" + name + '\'' +
                    ", age=" + age +
                    '}';
        }
    }
}
